package com.ting.test.algorithms.array;

import java.util.HashMap;

/**
 * Created by 雷霆 on 2020/3/11.
 * <p>
 * Description：
 * 给两个整数数组 A 和 B ，返回两个数组中公共的、长度最长的子数组的长度。
 * <p>
 * 示例 1:
 * <p>
 * 输入:
 * A: [1,2,3,2,1]
 * B: [3,2,1,4,7]
 * 输出: 3
 * 解释:
 * 长度最长的公共子数组是 [3, 2, 1]。
 * 说明:
 * 1 <= len(A), len(B) <= 1000
 * 0 <= A[i], B[i] < 100
 */
public class MaxLengthSubArray {
    public static int findLength(int[] A, int[] B) {
        if (null == A || null == B) {
            return 0;
        }
        int lengthA = A.length;
        int lengthB = B.length;
        if (lengthA < 1 || lengthB < 1) {
            return 0;
        }

        HashMap map = new HashMap(100);

        for (int i = 0; i < lengthA; i++) {
            map.put(A[i], 1);
        }
        int count = 0;
        for (int i = 0; i < lengthB; i++) {
            if (null != map.get(B[i])) {
                count++;
            } else {
                map.put(B[i], 1);
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] A = new int[]{0,0,0,0,1};
        int[] B = new int[]{1,0,0,0,0};
        int length = findLength(A, B);
        System.out.println(length);
    }

}
